package com.qf.oop.book;

import java.lang.annotation.Retention;
import java.util.Scanner;

/**
 * 图书管理的子类
 * 基于Book数组进行图书管理
 * 图书的数据没有进行持久化存储
 */
public class BookServiceImpl extends BookService {

    private static Book[] bookArr = new Book[10];

    @Override
    public boolean addBook(Book book) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入图书的名字:");
        book.setName(sc.next());

        System.out.println("请输入类别:");
        book.setType(sc.next());

        System.out.println("请输入图书的作者:");
        book.setAuthor(sc.next());

        System.out.println("请输入id:");
        int id = sc.nextInt();
        if (isOnly(id)) {
            book.setId(id);
        } // id唯一可取用
        else {
            System.out.println("该id已存在");
        }

        System.out.println("请输入价格:(元)");
        book.setPrice(sc.nextDouble());

        for (int i = 0; i < bookArr.length; i++) {
            if (bookArr[i] == null) {
                bookArr[i] = book;
                return true;
            }
        }
        return false;
    }

    /**
     * id 是否唯一
     *
     * @param id
     * @return
     */
    public boolean isOnly(int id) {
        for (int i = 0; i < bookArr.length; i++) {
            if (bookArr[i] != null && bookArr[i].getId() == id)
                return false;
        }

        return true;
    }

    @Override
    public boolean deleteBook(int id) {
        for (int i = 0; i < bookArr.length; i++) {
            if (bookArr[i] != null && bookArr[i].getId() == id) {
                bookArr[i] = null;
            }
        }

        return true;
    }

    @Override
    public void updateBook(int id) {
        Scanner sc = new Scanner(System.in);
        Book book = new Book();

        for (int i = 0; i < bookArr.length; i++) {
            if (bookArr[i] != null && bookArr[i].getId() == id) {

                book.setId(id);
                System.out.println("请输入图书的名字");
                book.setName(sc.next());
                System.out.println("请输入类别");
                book.setType(sc.next());
                System.out.println("请输入图书的作者");
                String author = sc.next();
                book.setAuthor(author);
                System.out.println("请输入价格:(元)");
                book.setPrice(sc.nextDouble());

                printBookInfo(book);
                bookArr[i] = book;


                return;
            }
        }

        System.out.println("输入id不正确");
    }


    @Override
    public Book findBook(int id) {
        for (int i = 0; i < bookArr.length; i++) {
            if (bookArr[i] != null && bookArr[i].getId() == id) {
                printBookInfo(bookArr[i]);
                return bookArr[i];
            }
        }

        return null;
    }

    @Override
    public Book[] findBookByAuthor(String author) {
        Book[] books = new Book[10];
        for (int i = 0; i < bookArr.length; i++) {
            if (bookArr[i].getAuthor().equals(author))
                books[i] = bookArr[i];
        }
        // 检查返回数组是否为空
        if (checkIsNull(books)) return null;


        printBookInfo(books);
        return books;
    }

    @Override
    public Book[] findBookByName(String bookname) {
        Book[] books = new Book[10];
        for (int i = 0; i < bookArr.length; i++) {
            if (bookArr[i] != null && bookArr[i].getName().equals(bookname))
                books[i] = bookArr[i];
            else continue;
        }

        // 检查返回数组是否为空
        if (checkIsNull(books)) return null;

        printBookInfo(books);
        return books;
    }

    /**
     * 检查返回数组是否为空
     *
     * @param books
     * @return 是空返回 true 否则返回 false
     */
    public boolean checkIsNull(Book[] books) {
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) return false;
        }
        return true;
    }

    public void printBookInfo(Book book) {
        System.out.print("书名:" + book.getName());
        System.out.print(" 类别:" + book.getType());
        System.out.print(" 作者:" + book.getAuthor());
        System.out.print(" id:" + book.getId());
        System.out.println(" 价格:" + book.getPrice());
    }

    public void printBookInfo(Book[] books) {
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) printBookInfo(books[i]);
        }
    }
}
